package com.lhy.controller;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lhy.domain.Friend;
import com.lhy.domain.Page;
import com.lhy.domain.Result;
import com.lhy.domain.Student;
import com.lhy.service.IUserService;



@Controller
@RequestMapping("/findfriend")
public class FindFriendController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping("")
	public ModelAndView list(HttpSession s,String pageNow){
		Integer pagenow = 1;
		if(!StringUtils.isEmpty(pageNow)){
			pagenow = Integer.valueOf(pageNow);
		}
		ModelAndView mv = new ModelAndView();
		try {
			Integer sequence = Integer.valueOf(s.getAttribute("usersequence").toString());
			Integer friendCount = userService.queryAllFriendCount(sequence);
			if(friendCount>0){
				List<Friend> friendList = userService.queryAllFriend(sequence);
                List<Integer> LString = new ArrayList<Integer>();

				Integer[]friendSequences = null;
				for (Friend friend : friendList) {
					LString.add(friend.getSequence());
				}
				int size = LString.size();
				friendSequences = (Integer[]) LString.toArray(new Integer[size]);
				Integer userCount = userService.queryAllUserCountDeleteFriends(sequence,friendSequences);
				Page page = new Page(userCount, pagenow);
				Integer startIndex = pagenow*page.getPageSize()-7;
				Integer endIndex = pagenow*page.getPageSize();
				List<Student> list = userService.queryAllUserByPageDeleteFriends(sequence,friendSequences,startIndex,endIndex);
				mv.addObject("list",list);
				mv.addObject("page",page);
				mv.setViewName("findfriend");
			}else{
				Integer userCount = userService.queryAllUserCount(sequence);
				Page page = new Page(userCount, pagenow);
				Integer startIndex = pagenow*page.getPageSize()-7;
				Integer endIndex = pagenow*page.getPageSize();
				List<Student> list = userService.queryAllUserByPage(sequence,startIndex,endIndex);
				mv.addObject("list",list);
				mv.addObject("page",page);
				mv.setViewName("findfriend");
			}
			
			
			

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping("/do")
	public ModelAndView user(HttpSession s,String username,String sequence){

		ModelAndView mv = new ModelAndView();
		try {
			Student user =new Student();
			
			if(!StringUtils.isEmpty(username)){
				user = userService.queryStudentByUsername(username);
			}else if(!StringUtils.isEmpty(sequence)){
				user = userService.queryStudentBySequence(Integer.valueOf(sequence));
			}
			mv.addObject("user",user);
			mv.setViewName("findfriend");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping("/addUser")
	public @ResponseBody
	Map<String, Object> add(String usersequence, String sequence , String remark, String message)  {
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		Integer state = 1;// 1申请  
		try{
			Date date = new Date();       
			Timestamp create_dt = new Timestamp(date.getTime());
			Integer userSequence = Integer.valueOf(usersequence);
			Student u = userService.queryStudentBySequence(userSequence);
			Student friend = userService.queryStudentBySequence(Integer.valueOf(sequence));
			if(StringUtils.isEmpty(remark)){
				remark = friend.getNickname();
			}
			userService.findFriendApply(Integer.valueOf(sequence),userSequence,u.getUserid(),u.getNickname(),u.getEmail(),u.getPhonenumber(),remark,message,create_dt,u.getSex(),state,u.getImg());				
			result.setDes("成功");
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		returnMap.put("result", result);
		return returnMap;
	}
	@RequestMapping("/check")
	public @ResponseBody
	Map<String, Object> check(String username)  {
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");

		try{
			Integer count = userService.queryCountByUsername(username);
			returnMap.put("count", count);
		}catch(Exception e){
			e.printStackTrace();
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		returnMap.put("result", result);
		return returnMap;
	}
}
